/**
 * Copyright 2019, SumUp Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { describe, expect, it } from 'vitest';
import { createRef } from 'react';

import { render, screen } from '../../../../util/test-utils.js';

import { Tab } from './Tab.js';

describe('Tab', () => {
  it('should merge a custom class name with the default ones', () => {
    const className = 'foo';
    render(<Tab className={className} />);
    const element = screen.getByRole('tab');
    expect(element.className).toContain(className);
  });

  it('should forward a ref', () => {
    const ref = createRef<HTMLButtonElement>();
    render(<Tab ref={ref} />);
    const tab = screen.getByRole('tab');
    expect(ref.current).toBe(tab);
  });
  it('should render with tab semantics', () => {
    const { rerender } = render(<Tab>Tab title</Tab>);
    expect(screen.getByText('Tab title')).toHaveRole('tab');
    expect(screen.getByText('Tab title')).toHaveAttribute(
      'aria-selected',
      'false',
    );
    expect(screen.getByText('Tab title')).toHaveAttribute('tabindex', '-1');
    rerender(<Tab selected>Tab title</Tab>);
    expect(screen.getByText('Tab title')).toHaveAttribute(
      'aria-selected',
      'true',
    );
    expect(screen.getByText('Tab title')).not.toHaveAttribute('tabindex');
  });
  it('should render with listitem semantics', () => {
    const { rerender } = render(<Tab as="listitem">Tab title</Tab>);
    expect(screen.getByText('Tab title').parentElement).toHaveRole('listitem');
    expect(screen.getByText('Tab title')).not.toHaveAttribute('aria-selected');
    expect(screen.getByText('Tab title')).not.toHaveAttribute('tabindex');
    rerender(
      <Tab as="listitem" selected>
        Tab title
      </Tab>,
    );
    expect(screen.getByText('Tab title')).toHaveAttribute(
      'aria-current',
      'page',
    );
  });
});
